#include<bits/stdc++.h>
#define int long long
using namespace std;

int T, n, a[200010], ans, c[21], maa;

inline void solve();
inline void dfs(int p);
inline void dfs(int p, int r, int b);

inline void read(int& n){
    n = 0;
    int f = 1;
    char c;
    for(c = getchar(); c > '9' || c < '0'; c = getchar())
        if(c == '-')
            f = -1;
    for(; c >= '0' && c <= '9'; c = getchar())
        n = n * 10 + c - '0';
    n *= f;
}

signed main(){
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    read(T);
    for(; T--;)
        solve();
}

int f[110][110][110];

inline void solve(){
    read(n), maa = -1;
    for(int i = 1; i <= n; i++)
        read(a[i]), maa = max(maa, a[i]);
    if(n <= 15){
        ans = 0, dfs(1);
        printf("%lld\n", ans);
        return;
    }
    if(n <= 100 && maa <= 100){
        memset(f, 255, sizeof(f));
        f[0][0][0] = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j <= maa; j++)
                for(int k = 0; k <= maa; k++){
                    if(f[i][j][k] < 0)
                        continue;
                    if(a[i + 1] == j)
                        f[i + 1][a[i + 1]][k] = max(f[i + 1][a[i + 1]][k], f[i][j][k] + a[i + 1]);
                    else
                        f[i + 1][a[i + 1]][k] = max(f[i + 1][a[i + 1]][k], f[i][j][k]);

                    if(a[i + 1] == k)
                        f[i + 1][j][a[i + 1]] = max(f[i + 1][j][a[i + 1]], f[i][j][k] + a[i + 1]);
                    else
                        f[i + 1][j][a[i + 1]] = max(f[i + 1][j][a[i + 1]], f[i][j][k]);

                }
         ans = -1;
         for(int i = 0; i <= maa; i++)
             for(int j = 0; j <= maa; j++)
                 ans = max(ans, f[n][i][j]);
          printf("%lld\n", ans);
    }
}

inline void dfs(int p){
    if(p == n + 1){
        int res = 0;
        for(int i = 1; i <= n; i++){
            int id = -1;
            for(int j = i - 1; j && id == -1; j--)
                if(c[j] == c[i])
                    id = j;
            if(a[i] == a[id])
                res += a[i];
        }
        ans = max(ans, res);
        return;
    }
    c[p] = 0, dfs(p + 1);
    c[p] = 1, dfs(p + 1);
}
